package com.dhnsoft.代码随想录.哈希表;

import java.security.Key;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * ClassName: d06_383_赎金信
 * Package: com.dhnsoft.代码随想录.哈希表
 * Description:
 *
 * @Author dhn
 * @Create 2024/3/14 11:19
 * @Version 1.0
 */
public class d06_383_赎金信 {

    public boolean canConstruct(String ransomNote, String magazine) {
        Map<Character,Integer> map = new HashMap<>();
        char[] c1 = magazine.toCharArray();
        char[] c2 = ransomNote.toCharArray();
        for (char c : c2) {
            map.put(c,map.getOrDefault(c,0)+1);
        }

        for (char c : c1) {
            if(map.containsKey(c)){
                map.put(c,map.get(c)-1);
            }
        }
        Set<Character> characters = map.keySet();
        for (Character character : characters) {
            if(map.get(character)>0){
                return false;
            }
        }
        return true;

    }
}
